package com.qiaofei.controller;

import com.qiaofei.pojo.*;
import com.qiaofei.service.impl.EstateServiceImpl;
import com.qiaofei.service.impl.NewHouseServiceImpl;
import com.qiaofei.service.impl.OldHouseServiceImpl;
import com.qiaofei.service.impl.RentServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@Slf4j
@Controller
public class IndexController {

    @Autowired
    private EstateServiceImpl estateService;
    @Autowired
    private RentServiceImpl rentService;
    @Autowired
    private NewHouseServiceImpl newHouseService;
    @Autowired
    private OldHouseServiceImpl oldHouseService;

    private List<Estate> estateList = null;

    @GetMapping(value = {"/","/index.html"})
    public String index(Model model){
        log.info("首页 ：{}", "");

        estateList = estateService.list();
        PageBean rentPage = rentService.page(1,8,null, null, null);

        PageBean newHousePage = newHouseService.page(1,8,null, null, null);

        PageBean oldHousePage = oldHouseService.page(1,8,null, null, null);

        model.addAttribute("title", "房产中介");
        model.addAttribute("estateList", estateList);
        model.addAttribute("rentPage", rentPage);
        model.addAttribute("newHousePage", newHousePage);
        model.addAttribute("oldHousePage", oldHousePage);

        return "index";
    }

    @GetMapping(value = {"/pro_zu.html","/pro_zu.html/{num}"})
    public String pro_zu(@RequestParam(defaultValue = "") Integer estate,
                         @RequestParam(defaultValue = "") String area,
                         @RequestParam(defaultValue = "") String type,
                         @RequestParam(defaultValue = "") String sort,
                         @RequestParam(defaultValue = "asc") String sort_type,
                         @RequestParam(defaultValue = "1") Integer num,
                         Map<String, Object> map){
        log.info("出租 ：{}", "");

        estateList = estateService.list();
        Map<String, Object> params = new HashMap<>();
        params.put("estate", estate);
        params.put("area", area);
        params.put("type", type);
        params.put("sort", sort);
        params.put("sort_type", sort_type);
        params.put("state", 1);
        Integer pageSize = 10;

        PageBean house = rentService.page(num,pageSize, params, null, null);
        map.put("house", house);
        map.put("num", num);
        map.put("pageSize", pageSize);
        map.put("type", "rent");
        map.put("title", "房产出租");
        map.put("estateList", estateList);

        return "pro_zu";
    }

    @GetMapping(value = {"/pro_er.html","/pro_er.html/{num}"})
    public String pro_er(@RequestParam(defaultValue = "") Integer estate,
                         @RequestParam(defaultValue = "") String area,
                         @RequestParam(defaultValue = "") String type,
                         @RequestParam(defaultValue = "") String sort,
                         @RequestParam(defaultValue = "asc") String sort_type,
                         @RequestParam(defaultValue = "1") Integer num,
                         Map<String, Object> map){
        log.info("二手 ：{}", "");

        estateList = estateService.list();
        Map<String, Object> params = new HashMap<>();
        params.put("estate", estate);
        params.put("area", area);
        params.put("type", type);
        params.put("sort", sort);
        params.put("sort_type", sort_type);
        params.put("state", 1);
        Integer pageSize = 10;

        PageBean house = oldHouseService.page(num,pageSize, params, null, null);
        map.put("house", house);
        map.put("num", num);
        map.put("pageSize", pageSize);
        map.put("type", "old");
        map.put("title", "二手房交易");
        map.put("estateList", estateList);

        return "pro_er";
    }

    @GetMapping(value = {"/pro_xin.html","/pro_xin.html/{num}"})
    public String pro_xin(@RequestParam(defaultValue = "") Integer estate,
                         @RequestParam(defaultValue = "") String area,
                         @RequestParam(defaultValue = "") String type,
                         @RequestParam(defaultValue = "") String sort,
                         @RequestParam(defaultValue = "asc") String sort_type,
                         @RequestParam(defaultValue = "1") Integer num,
                         Map<String, Object> map){
        log.info("新房 ：{}", "");

        estateList = estateService.list();
        Map<String, Object> params = new HashMap<>();
        params.put("estate", estate);
        params.put("area", area);
        params.put("type", type);
        params.put("sort", sort);
        params.put("sort_type", sort_type);
        params.put("state", 1);
        Integer pageSize = 10;

        PageBean house = rentService.page(num,pageSize, params, null, null);
        map.put("house", house);
        map.put("num", num);
        map.put("pageSize", pageSize);
        map.put("type", "new");
        map.put("title", "新房交易");
        map.put("estateList", estateList);

        return "pro_xin";
    }

    @GetMapping("/proinfo.html/{type}/{id}")
    public String proinfo(@PathVariable("type") String type, @PathVariable("id") Integer id,  Model model){
        log.info("房源详情 ：{}", id);

        model.addAttribute("title", "房源详情");
        model.addAttribute("estateList", estateList);
        model.addAttribute("type", type);
        switch (type){
            case "rent":
                List<Rent> rentList = rentService.getByIds(Arrays.asList(new Integer[]{id}));
                Rent rent = rentList.get(0);
                model.addAttribute("info", rent);
                System.out.println(rent.getCreateTime());
                break;
            case "new":
                List<NewHouse> newHouseList = newHouseService.getByIds(Arrays.asList(new Integer[]{id}));
                NewHouse newHouse = newHouseList.get(0);
                model.addAttribute("info", newHouse);
                break;
            case "old":
                List<OldHouse> oldHouseList = oldHouseService.getByIds(Arrays.asList(new Integer[]{id}));
                OldHouse oldHouse = oldHouseList.get(0);
                model.addAttribute("info", oldHouse);
                break;
            default:
                return "error";
        }

        return "proinfo";
    }


}
